package org.hhchat.leetcode.string;

/**
 * Created by hh23485 on 2017/8/27.
 * https://leetcode.com/problems/longest-common-prefix/
 * 注意空串
 */
public class code14_easy {
    static class Solution {
        public String longestCommonPrefix(String[] strs) {
            if (strs.length == 0) {
                return "";
            }
            StringBuilder stringBuilder = new StringBuilder();
            int index = 0;
            char curChar;
            for (int i = 0; i < 500; i++) {
                if(index>=strs[0].length()){
                    break;
                }
                curChar = strs[0].charAt(index);
                for (int j = 0; j < strs.length; j++) {
                    if(index>=strs[j].length() || curChar!=strs[j].charAt(index)){
                        return stringBuilder.toString();
                    }
                }
                stringBuilder.append(curChar);
                index++;
            }
            return stringBuilder.toString();
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] strings = {"123456", "123"};
        System.out.println(solution.longestCommonPrefix(strings));
    }
}
